﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using HinduAlmanac.CalenderProper;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using HinduAlmanac.ViewModels;



namespace HinduAlmanac
{
    public partial class MainPage : PhoneApplicationPage
    {
        bool newPageInstanse = false;
        CalendarMethods objcalmethods = new CalendarMethods();
        CalenderData objcaldata = new CalenderData();
        public MainPage()
        {
            InitializeComponent();
            lstpicMonthNames.SelectionChanged += new SelectionChangedEventHandler(lstpicMonthNames_SelectionChanged);
            newPageInstanse = true;
        }



        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (newPageInstanse)
            {
                LoadCalendarData(DateTime.Now);
                //festivaldata();
            }
            newPageInstanse = false;
        }

        private void LoadCalendarData(DateTime dc)
        {
            CalenderData objcaldata = objcalmethods.Calenderdata(dc); 
            tblkTidi.Text = objcaldata.Tidi;
            tblkNakshatram.Text = objcaldata.Nakshatram;
            tblkWeekDay.Text = objcaldata.WeekDay;
            lbxVarjam.ItemsSource = objcalmethods.GetVajram(dc);
            lbxDurmohortam.ItemsSource = objcalmethods.GetDurmurtham(dc);
            lstpicMonthNames.ItemsSource = objcalmethods.LoadMonthsInListPicker();
            lstpicMonthNames.SelectedIndex = DateTime.Now.Month - 1;

        }

        void lstpicMonthNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstpicMonthNames.SelectedIndex == -1)
                return;
            int monthId = (lstpicMonthNames.SelectedItem as Months).MonthId;
            lbxFestivals.ItemsSource =objcalmethods.GetMonthId(monthId);
        }

        private void festivaldata()
        {
            XDocument xdoc = null;
            xdoc = XDocument.Load("AppData/CalenderDetails/Festivals.xml");
            var festilist = from i in xdoc.Descendants("festival")
                            select i;
            List<FestivalProper> objfest = new List<FestivalProper>();

            foreach (var fest in festilist)
            {
                FestivalProper objfe = new FestivalProper();

                objfe.FId = fest.Attribute("id").Value;
                objfe.FName = fest.Element("name").Value;
                objfe.MonthName = fest.Element("date").Value;

                objfest.Add(objfe);
            }

        }

        private void dpicCalender_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            DateTime dc = Convert.ToDateTime(dpicCalender.Value);
          
           LoadCalendarData(dc);
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            dpicCalender.Value = Convert.ToDateTime(dpicCalender.Value).AddDays(-1);
        }
        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            dpicCalender.Value = Convert.ToDateTime(dpicCalender.Value).AddDays(+1);

        }


        #region Links of about us Pivot Item

        private void facebookImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SbFacebook.Completed += new EventHandler(SbFacebook_Completed);
        }

        void SbFacebook_Completed(object sender, EventArgs e)
        {
            LoadBrowserTask("http://on.fb.me/eytwPa");
        }

        private void blogImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SbBlogger.Completed += new EventHandler(SbBlogger_Completed);
        }

        void SbBlogger_Completed(object sender, EventArgs e)
        {
            LoadBrowserTask("http://bit.ly/hpoWQX");
        }

        private void twitterImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SbTwitter.Completed += new EventHandler(SbTwitter_Completed);
        }

        void SbTwitter_Completed(object sender, EventArgs e)
        {
            LoadBrowserTask("http://bit.ly/exHPzZ");
        }

        private void youtubeImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SbYoutube.Completed += new EventHandler(SbYoutube_Completed);
        }

        void SbYoutube_Completed(object sender, EventArgs e)
        {
            LoadBrowserTask("http://bit.ly/eYf4xG");
        }

        private void LoadBrowserTask(string url)
        {
            NavigationService.Navigate(new Uri("/Webbrowser/WebBrowserPage.xaml?url=" + url, UriKind.Relative));
        }

        //private void lbxAbout_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (lbxAbout.SelectedIndex != -1)
        //    {
        //        int SelIndex = lbxAbout.SelectedIndex;
        //        lbxAbout.SelectedIndex = -1;
        //        switch (SelIndex)
        //        {
        //            case 0:
        //                //rate this app
        //                MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
        //                marketplaceReviewTask.Show();
        //                break;
        //            case 1:
        //                //share this app
        //                EmailComposeTask emailcomposertask = new EmailComposeTask();
        //                emailcomposertask.To = "";
        //                emailcomposertask.Subject = "Activity Tracker App";
        //                string lnk = "I have recently found a very good productivity app that I would like to share with you.\n";
        //                lnk = lnk + "Below is the link of the app.\n\n\n";
        //                lnk = lnk + "From Windows Phone 7.\n";
        //                lnk = lnk + "http://social.zune.net/redirect?type=phoneApp&id=ebe21744-8623-e011-854c-00237de2db9e";
        //                lnk = lnk + "\n\n\nFrom Windows PC\n";
        //                lnk = lnk + "zune://navigate/?appID=ebe21744-8623-e011-854c-00237de2db9e";
        //                emailcomposertask.Body = lnk;
        //                emailcomposertask.Show();
        //                break;
        //            case 2:
        //                //related apps
        //                MarketplaceSearchTask marketplaceSearchTask = new MarketplaceSearchTask();
        //                marketplaceSearchTask.SearchTerms = "lartsoft";
        //                marketplaceSearchTask.Show();
        //                break;
        //            case 3:
        //                //Feedback
        //                NavigationService.Navigate(new Uri("/Feedback.xaml", UriKind.Relative));
        //                break;
        //        }
        //    }
        //}


        #endregion



        private void lbxFestivals_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxFestivals.SelectedIndex == -1)
                return;
            NavigationService.Navigate(new Uri("/Views/FestivalDetails.xaml?festid=" + (lbxFestivals.SelectedItem as FestivalProper).FId, UriKind.Relative));
        }

    }
}

